package Trip;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class Trip {
	private int id;
	public Transport transport;
	private TripPurpose tripPurpose;
	private Feeding feeding;
	private int daysDuration = 0;

	private static Logger log = Logger.getLogger(Trip.class.getName());

	Properties properties = new Properties();
	Properties properties2 = new Properties();

	public static class Builder {
		private int id;
		private Transport transport;
		private TripPurpose tripPurpose;
		private Feeding feeding;
		private int daysDuration = 0;

		public Builder() {
			super();
		}

		public Builder id(int id) {
			this.id = id;
			return this;
		}

		public Builder transport(Transport t) {
			transport = t;
			return this;
		}

		public Builder tripPurpose(TripPurpose t) {
			tripPurpose = t;
			return this;
		}

		public Builder feeding(Feeding f) {
			feeding = f;
			return this;
		}

		public Builder daysDuration(int i) {
			daysDuration = i;
			return this;
		}

		public Trip build() {
			return new Trip(this);
		}

	}

	public Trip() {
	}

	public Trip(Builder builder) {
		id = builder.id;
		transport = builder.transport;
		tripPurpose = builder.tripPurpose;
		feeding = builder.feeding;
		daysDuration = builder.daysDuration;
		createTour();
	}

	public void createTour() {
		properties.setProperty("id", this.id + "");
		properties.setProperty("transport", this.transport.getTransport() + "");
		properties.setProperty("tripPurpose", this.tripPurpose.getTripPurpose()
				+ "");
		properties.setProperty("feeding", this.feeding.getFeedingType() + "");
		properties.setProperty("daysDuration", this.daysDuration + "");
		try {
			properties.store(new FileOutputStream("CreatedTours.properties"),
					null);
		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
	}

	public void findTour() {
		try {

			properties.load(new FileInputStream("DesiredTour.properties"));
			String a = properties.getProperty("tripPurpose");
			switch (a) {
			case "SHOPPING":
				properties2
						.load(new FileInputStream("ShoppingTour.properties"));
				break;
			case "RECREATION":
				properties2.load(new FileInputStream(
						"RecreationalTour.properties"));
				break;
			case "SANATORIUM":
				properties2.load(new FileInputStream(
						"SanatoriumTour.properties"));
				break;
			}

			if (checkFields()) { 
				log.info("Matching tour id is " + properties2.getProperty("id"));
				
			} else {
				log.info("No matching tours");

			}

		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
	}

	public boolean checkFields() {
		if (properties2.getProperty("transport").equals(
				properties.getProperty("transport"))
				&& properties2.getProperty("daysDuration").equals(
						properties.getProperty("daysDuration"))
				&& properties2.getProperty("feeding").equals(
						properties.getProperty("feeding"))) {
			return true;
		}
		return false;

	}
	
}
